package com.sdp.th.netty.utils;

import java.math.BigInteger;

public class NettyUtils {

	public static final int MESSAGE_MAX_LENGTH = 200;// 最多200

	public static final int MESSAGE_MIN_LENGTH = 86;// 最少86

	// 温湿度报文长度
	public static final int MESSAGE_TH_LENGTH = 86;

	public static final String MESSAGE_START = "0146";

	public static final String ACTOR_FIX = "IMEI";

	/**
	 * 16进制转ASCII
	 * 
	 * @param hexStr
	 * @return
	 */
	public static String hexToAscii(String hexStr) {
		StringBuilder output = new StringBuilder("");
		for (int i = 0; i < hexStr.length(); i += 2) {
			// 每两个字符分为一组
			String str = hexStr.substring(i, i + 2);
			// 将每组的16进制数转换为对应的ASCII字符
			char asciiChar = (char) Integer.parseInt(str, 16);
			// 添加到结果字符串中
			output.append(asciiChar);
		}
		return output.toString();
	}

	/**
	 * 数组转16进制
	 * 
	 * @param bytes
	 * @return
	 */
	public static String bytesToHex(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			sb.append(String.format("%02X", b));
		}
		return sb.toString();
	}

	public static String hexToDecimal(String hexString) {
		BigInteger decimalValue = new BigInteger(hexString, 16); // 将16进制字符串转换为BigInteger对象
		return decimalValue.toString(); // 直接获取BigInteger对象的字符串表示（即10进制字符串）
	}

	/**
	 * 得到错误信息
	 * 
	 * @param exceptionName
	 * @param exceptionMessage
	 * @param elements
	 * @return
	 */
	public static String stackTraceToString(String exceptionName, String exceptionMessage,
			StackTraceElement[] elements) {
		StringBuffer strbuff = new StringBuffer();
		for (StackTraceElement stet : elements) {
			strbuff.append(stet + "\n");
		}
		String message = exceptionName + ":" + exceptionMessage + "\n\t" + strbuff.toString();
		return message;
	}
}
